package com.ee.bruscar.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ee.bruscar.model.dto.AccountDetails;
import com.ee.bruscar.model.dto.ActiveGame;
import com.ee.bruscar.model.dto.Address;
import com.ee.bruscar.model.dto.Customer;

public class CustomerServiceStubTarget implements CustomerService
{
	private Map<String,String> hintMap;

	public CustomerServiceStubTarget()
	{
		hintMap = new HashMap<String, String>();
		hintMap.put("Customer.firstName", "Ed,Ralf,Andrei,Ignacio,Marty");
		hintMap.put("Customer.lastName", "Bokelberg,Eustace,Martin,Pitt,Jurescu");
		hintMap.put("Address.city", "Berlin,London,Madrid,Cruj Napoca");
		hintMap.put("Address.country", "Germany,Ireland,Spain,UK,Romania");
		
	}
	
	public List<ActiveGame> getActiveGames(String id, String type)
	{
		// TODO Auto-generated method stub
		return null;
	}

	public Customer getCustomer(String id)
	{
		return createStubCustomer();
	}
	
	/*
	 * private ${ComplexObject} createStub${ComplexObject}()
	 * {
	 * 	 ${ComplexObject} stub = new ${ComplexObject}();
	 *   for( setter : ${Setters} )
	 *   {
	 *   	if( isSimple( setter ) )
	 *   	{
	 *   		stub.set${setterName}( getStubFor( ${ComplexObject}.${setterName} ) );
	 *   	}
	 *      else
	 *      {
	 *      	stub.set${setterName}( createStub${setterType}() );
	 *      }
	 *   }
	 * }
	 */
	private Customer createStubCustomer()
	{
		Customer stub = new Customer();
		stub.setFirstName(getStubFor( "Customer.firstName" ));
		stub.setLastName( getStubFor( "Customer.lastName" ));
		stub.setAddress( createStubAddress() );
		return stub;
	}

	private Address createStubAddress()
	{
		Address stub = new Address();
		stub.setCity( getStubFor( "Address.city"));
		stub.setCountry( getStubFor( "Address.country"));
		return stub;
	}

	private String getStubFor( String fieldName )
	{
		if( hintMap.containsKey(fieldName) )
		{
			return DataMaker.createData( hintMap.get( fieldName ));
		}
		
		return fieldName;
	}

	public AccountDetails getDetails(String id)
	{
		// TODO Auto-generated method stub
		return null;
	}
	
	public static class DataMaker
	{
		public static String createData(String hint)
		{
			String[] items = hint.split(",");
			int index = getRandomIndex( items.length );
			return items[index];
		}

		private static int getRandomIndex(int length)
		{
			double random = Math.random();
			double multiplied = random * length;
			
			Double dbl = new Double( multiplied );
			return dbl.intValue();
		}
	}


}
